package com.dkd.day20250617;

import com.dkd.day20250614.ListNode;

public class reverseBetween {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @param m int整型
     * @param n int整型
     * @return ListNode类
     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        //设置虚拟头节点
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode prev = dummy;
        for (int i = 0; i <= m-1; i++) {
            prev = prev.next;
        }
        ListNode cur = prev.next;
        ListNode Cur_next;
        for (int i = 0; i <= m-n; i++) {
            //cur节点不变，但是不段在往后移动
            //1将cur的下一个节点赋值给Cur_next
            Cur_next = cur.next;
            //2将cur节点的next改为下下个节点
            cur.next = Cur_next.next;
            //3将next的节点的next改为pre.next，第一次即为cur
            Cur_next .next = prev.next;
            //4插入next节点
            prev.next = Cur_next ;

        }
        return dummy.next;
    }
}
